<template>
  <!--列表-->
  <router-exact-view name="school">
    <div style="width: 100%" class="template-list-page-container">
      <my-table
        v-loading="loading"
        :columns="columns"
        :data-source="data"
        :pagination="pagination"
        :btn-options="btnOptions"
        title="学校列表"
        @page-change="handlePageChange"
      >
        <div slot="table-btn">
          <el-button type="primary" @click="addSchool">新增学校</el-button>
        </div>
      </my-table>
    </div>
  </router-exact-view>
</template>

<script>
import { getSchool } from '@/api/basic'
import MyTable from '@/components/my-table/index'
import RouterExactView from '@/components/routerExactView'
import { schoolColumns } from '../columns'

export default {
  components: { RouterExactView, MyTable },
  data() {
    const that = this
    return {
      /* 列表相关 */
      columns: schoolColumns,
      data: [],
      searchForm: {
      },
      loading: false,
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0
      },
      btnOptions: [
        {
          name: '查看详情',
          handler: function(row) {
            that.$router.push({ path: `/basic/teacher/detail/${row.id}` })
          }
        },
        {
          name: '编辑',
          handler: function(row) {
            that.$router.push({ path: `/basic/school/edit/${row.id}` })
          }
        }
      ]
    }
  },
  mounted() {
    if (this.$route.name === 'school') {
      this.handlePageChange()
    }
  },
  methods: {
    async handlePageChange(current, size) {
      if (current) {
        this.pagination.current = current
      }
      if (size) {
        this.pagination.pageSize = size
      }
      this.loading = true
      try {
        const res = await getSchool({
          pageNum: this.pagination.current,
          pageSize: this.pagination.pageSize
        })
        if (res && res.list && res.list.length) {
          this.data = res.list
          this.pagination = {
            current: res.pageNum,
            pageSize: res.pageSize,
            total: res.totalElements
          }
        }
        this.loading = false
      } catch (err) {
        this.loading = false
      }
    },
    resetSearchForm() {
      this.searchForm = {}
      this.handlePageChange(1)
    },
    addSchool() {
      this.$router.push({
        path: '/basic/school/add'
      })
    }
  }
}
</script>
<style scoped lang="less">
  @import "../../common.css";
  /* 查询条件容器的样式 */
  .search-area {
    padding: 16px 16px 0;
    margin-bottom: 8px;
    background: #fff;
  }
</style>
